package com.mive.controller;

import com.mive.entity.Author;
import com.mive.service.AuthorJpaService;
import com.mive.service.AuthorService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/author.do")
public class AuthorController {

    private Logger logger = LoggerFactory.getLogger(AuthorController.class);

    @Autowired
    private AuthorJpaService authorJpaService;
    @Autowired
    private AuthorService authorService;

    /***
     * 新增
     * @param author
     */
    @RequestMapping(value = "/create", method = RequestMethod.GET)
    public void create(Author author){
        author.setRealName("N01");
        author.setNickName("N01");
        logger.info("<==The create new object Id is:==>"+author.getId());
        authorJpaService.create(author);
    }

    /***
     * 查询全部
     * @return
     */
    @RequestMapping(value = "/findAll", method = RequestMethod.GET)
    public Map<String,Object> findAll(){
        List<Author> list = authorJpaService.findAll();
        Map<String,Object> reqMap = new HashMap<>();
        reqMap.put("total",list.size());
        reqMap.put("rows",list);
        logger.info("<==The findAll request result is:==>"+reqMap);
        return reqMap;
    }

    /***
     * 分页查询
     * @return
     */
    @RequestMapping(value = "/findAllByPage", method = RequestMethod.GET)
    public Page<Author> findAllByPage(){
        int page = 1;
        int size = 5;
        Sort sort = new Sort(Sort.Direction.DESC, "id");
        Pageable pageable = new PageRequest(page, size, sort);
        Page<Author> pageAuthor = authorJpaService.findAll(pageable);
        return pageAuthor;
    }

    /***
     * 根据id查询详情
     * @param uid
     * @return
     */
    @RequestMapping(value = "/findById/{uid:\\d+}",method = RequestMethod.GET)
    public Author findById(@PathVariable Long uid){
        Author author = new Author();
        author = authorJpaService.findAuthor(uid);
        return author;
    }

    /**
     * 根据姓名查找
     * @param realName
     * @return
     */
    @RequestMapping(value = "/findByName/{realName:\\d+}",method = RequestMethod.GET)
    public Author findByName(@PathVariable String realName){
        Author author = new Author();
        author = authorJpaService.findByName(realName);
        return author;
    }

    /***
     * 查询全部
     * @return
     */
    @RequestMapping(value = "/getAll", method = RequestMethod.GET)
    public Map<String,Object> getAll(){
        List<Author> list = authorService.getAll();
        Map<String,Object> reqMap = new HashMap<>();
        reqMap.put("total",list.size());
        reqMap.put("rows",list);
        logger.info("<==The findAll request result is:==>"+reqMap);
        return reqMap;
    }
}


